//
//  StockDetailDVC.m
//  MyStock
//
//  Created by 刘翔 on 2024/12/24.
//

#import "StockDetailDVC.h"
#import "StockDetailDCell.h"
#import "SeletedStock.h"

@interface StockDetailDVC ()

@property(nonatomic,strong)UILabel *titleL;
@end

@implementation StockDetailDVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    WeatherLabel *label = [[WeatherLabel alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
    label.attributedText = [[NSAttributedString alloc] initWithString:[AppService shareInstance].weather];
    [self.customNavBar addSubview:label];
    
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.customNavBar).offset(-10);
        make.right.equalTo(self.customNavBar).offset(-20);
        make.height.mas_equalTo(44);
        make.width.mas_equalTo(70);
    }];
    
    UIView *headerTitleView = [[UIView alloc] init];
    [self.customNavBar addSubview:headerTitleView];
    
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(0, 0, 20, 20);
    [leftBtn setImage:[UIImage imageNamed:@"shixinjiantouxiangzuo"] forState:UIControlStateNormal];
    [leftBtn setImage:[UIImage imageNamed:@"shixinjiantouxiangzuo-3"] forState:UIControlStateHighlighted];
    [leftBtn addTarget:self action:@selector(leftButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    [headerTitleView addSubview:leftBtn];
    
    self.titleL = [[UILabel alloc] init];
    self.titleL.font = [UIFont systemFontOfSize:16];
    self.titleL.textColor = [UIColor whiteColor];
    self.titleL.textAlignment = NSTextAlignmentCenter;
    [headerTitleView addSubview:self.titleL];
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(0, 0, 20, 20);
    [rightBtn setImage:[UIImage imageNamed:@"shixinjiantouxiangyou"] forState:UIControlStateNormal];
    [rightBtn setImage:[UIImage imageNamed:@"shixinjiantouxiangyou-2"] forState:UIControlStateHighlighted];

    [rightBtn addTarget:self action:@selector(rightButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    [headerTitleView addSubview:rightBtn];
    
    
    [headerTitleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(64);
        make.centerX.equalTo(self.customNavBar);
        make.bottom.equalTo(self.customNavBar);

    }];
    
    [leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.width.mas_equalTo(20);
        make.centerY.equalTo(headerTitleView);
        make.left.equalTo(headerTitleView);
    }];
    
    [self.titleL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(64);
        make.centerY.equalTo(headerTitleView);
        make.left.equalTo(leftBtn.mas_right).offset(5);
    }];
    
    [rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.width.mas_equalTo(20);
        make.centerY.equalTo(headerTitleView);
        make.left.equalTo(self.titleL.mas_right).offset(5);
        make.right.equalTo(headerTitleView);
    }];
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
//    self.collectionView.pagingEnabled = YES;
//    self.collectionView.scrollEnabled = NO;
    [self creatCollection];
    
    [self.view.layer needsLayout];
    [self.view.layer layoutIfNeeded];
    [self.collectionView reloadData];
    self.collectionView.contentOffset = CGPointMake(self.index * HWScreenW, 0);
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:self.index inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
    [self updateTitle];
}

- (void)updateTitle{
    id model = self.dataList[self.index];
    if([model isKindOfClass:[CandidateStock class]]){
        CandidateStock *cmodel = (CandidateStock *)model;
        self.titleL.text = cmodel.Name;
    }else if([model isKindOfClass:[SeletedStock class]]){
        SeletedStock *smodel = (SeletedStock *)model;
        self.titleL.text = smodel.name;
    }else if([model isKindOfClass:[PositionModel class]]){
        PositionModel *smodel = (PositionModel *)model;
        self.titleL.text = smodel.name;
    }else{
        StockModel *smodel = (StockModel *)model;
        self.titleL.text = smodel.candidate_stock.Name;
        
    }
}

- (void)leftButtonClicked{
    if(self.index == 0){
        self.index = self.dataList.count -1;
    }else{
        self.index -= 1;
    }
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:self.index inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
    [self updateTitle];
}

- (void)rightButtonClicked{
    if(self.index == self.dataList.count -1){
        self.index = 0;
    }else{
        self.index += 1;
    }
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:self.index inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
    [self updateTitle];
}

- (void)creatCollection
{
    
    self.layout.itemSize = CGSizeMake(kScreenWidth, kScreenHeight - Height_NavBar - kBottomSafeHeight );
    self.layout.minimumLineSpacing = 0.1;
    self.layout.minimumInteritemSpacing = 0.1;
    self.layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    [self.collectionView registerNib:[UINib nibWithNibName:@"StockDetailDCell" bundle:nil] forCellWithReuseIdentifier:@"StockDetailDCellID"];
    
}

#pragma mark - UICollectionViewDelegate
- (NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataList.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    StockDetailDCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"StockDetailDCellID" forIndexPath:indexPath];
    id model = self.dataList[indexPath.row];
    if ([model isKindOfClass:[CandidateStock class]]) {
        cell.model = model;
    }else if([model isKindOfClass:[SeletedStock class]]){
        SeletedStock *smodel = (SeletedStock *)model;
        if (smodel.candidate_stock.Code == nil) {
            cell.seletedStock = smodel;
        }else{
            cell.model = smodel.candidate_stock;

        }
    }else if([model isKindOfClass:[PositionModel class]]){
        cell.positionModel = model;
    }else{
        StockModel *smodel = (StockModel *)model;
        cell.model = smodel.candidate_stock;
    }
    return cell;
    
}

//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
//{
//
//    return CGSizeMake([self.dataList[indexPath.row] getWidthWithfont:[UIFont systemFontOfSize:13]] + 20, 22);
//}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
